package com.ice.body.business.controller;

import com.github.pagehelper.PageHelper;
import com.ice.base.controller.BaseController;
import com.ice.base.result.AjaxResult;
import com.ice.base.result.ResponseResult;
import com.ice.body.business.model.ReagentMsdsModel;
import com.ice.body.business.model.ReagentOwn;
import com.ice.body.business.service.ReagentMsdsService;
import com.ice.body.business.service.ReagentOwnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with GuoYanBin.
 * Description:
 * Date: 2023-04-04
 * Time: 05:14
 */
@RestController
@Api(tags = "单位内的试剂库")
public class ReagentOwnController extends BaseController{
    //---------------------------单位内的试剂库-----------------------start
    @Resource
    private ReagentOwnService reagentOwnService;
    @Resource
    private ReagentMsdsService reagentMsdsService;
    
    @ApiOperation(value = "单位内的试剂库-新增")
    @PostMapping("/api/ReagentOwn/insert")
    public AjaxResult<ReagentOwn> insert_ReagentOwn(ReagentOwn model) {
        AjaxResult r = AjaxResult.success();
        try{
            ReagentOwn own = reagentOwnService.getByCname(model.getCName());
            if(own != null){
                return AjaxResult.error("已存在同名危化品！");
            }
            Integer totalCount = model.getTotalCount();

            if(null == totalCount || totalCount<0){
                model.setTotalCount(0);
            }
            model.setCreateTime(new Date());
            reagentOwnService.insert(model);
        }catch (Exception e){
            e.printStackTrace();
            r = AjaxResult.error(); 
        }
        return r;
    }

    @ApiOperation(value = "将msds中的数据增加到私有库-新增")
    @PostMapping("/api/ReagentOwn/msdsToOwn")
    public AjaxResult<T> msdsToOwn(@RequestParam("id") String id) {
        AjaxResult r = AjaxResult.success();
        try{
            ReagentMsdsModel reagentMsdsModel = reagentMsdsService.selectById(id);
            if(null == reagentMsdsModel){
                return  AjaxResult.error("id错误");
            }
            String cName = reagentMsdsModel.getCName();
            ReagentOwn own = reagentOwnService.getByCname(cName);
            if(own != null){
                return AjaxResult.error("已存在同名危化品！");
            }
            own = new ReagentOwn();
            own.setCasNo(reagentMsdsModel.getCasNo());
            own.setCName(reagentMsdsModel.getCName());
            own.setTotalCount(0);
            own.setRE(reagentMsdsModel.getRE());
            own.setLower(10);
            own.setUpper(1000);
            own.setCreateTime(new Date());
            reagentOwnService.insert(own);
        }catch (Exception e){
            e.printStackTrace();
            r = AjaxResult.error();
        }
        return r;
    }

    @ApiOperation(value = "单位内的试剂库-删除")
    @PostMapping("/api/ReagentOwn/deleteById")
    public AjaxResult<ReagentOwn>  deleteById_ReagentOwn(@RequestParam("id") String id) {
        AjaxResult r = AjaxResult.success();
        try{
            reagentOwnService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            r = AjaxResult.error();  
        }
        return r;
    }

    @ApiOperation(value = "单位内的试剂库-更新")
    @PostMapping("/api/ReagentOwn/updateById")
    public AjaxResult<ReagentOwn> update_ReagentOwn(ReagentOwn model) {
        try{
            reagentOwnService.update(model);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();      
        }
        return AjaxResult.success();
    }

    @ApiOperation(value = "单位内的试剂库-查询一个")
    @PostMapping("/api/ReagentOwn/selectById")
    public AjaxResult<ReagentOwn> selectById_ReagentOwn(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
           ReagentOwn model = reagentOwnService.selectById(id);
           r.setData(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "单位内的试剂库-查询多个")
    @PostMapping("/api/ReagentOwn/selectAll")
    public AjaxResult<ReagentOwn> selectList_ReagentOwn(ReagentOwn model) {
        AjaxResult r = AjaxResult.success();
        try{
           List<ReagentOwn> list = reagentOwnService.selectListByModel(model);
           r.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "单位内的试剂库-查询分页")
    @PostMapping("/api/ReagentOwn/selectListByPageHelper")
    public AjaxResult<ReagentOwn> selectListByPageHelper_ReagentOwn(@RequestParam(name = "pageSize") Integer pageSize,
                                                         @RequestParam(name = "pageNum") Integer pageNum,
                                                         ReagentOwn model){ 
        AjaxResult r = AjaxResult.success();
        ArrayList list;
        try{
           PageHelper.startPage(pageNum, pageSize);
           list = reagentOwnService.selectListByModel(model);
           getReturnResultByPage(r,list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }
    //---------------------------单位内的试剂库-----------------------end
}
